package cn.swu.ghx.swu.library.fragment

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import cn.swu.ghx.swu.library.R
import cn.swu.ghx.swu.library.entity.Academy
import com.bumptech.glide.Glide
import com.google.android.material.carousel.CarouselLayoutManager

/**
 * A simple [Fragment] subclass.
 * Use the [AcademyFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class AcademyFragment : Fragment() {

    lateinit var adapter:AcademyAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_academy, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val carouselView = view.findViewById<RecyclerView>(R.id.carousel_recycler_view)
        carouselView.layoutManager = CarouselLayoutManager()
        carouselView.adapter = CarouseAdapter()

        //
        adapter =  AcademyAdapter()
        val recycler = view.findViewById<RecyclerView>(R.id.recycler)
        recycler.adapter = adapter

        loadAcademy()
    }

    private fun loadAcademy() {
        val data = listOf(
            Academy("计信院","http://cis.swu.edu.cn/__local/2/E4/60/8854C3C3190649263BFDD8DDF2E_50CA34DC_1257E.jpg"),
            Academy("数据与统计学院","http://math.swu.edu.cn/__local/0/C2/E2/1C171B011C45CC96389823392B8_78CCF7B6_7B6A6.png"),
            Academy("动物医学院","http://dwyx.swu.edu.cn/__local/E/D7/5B/819CCD1FFE21080E9857AB8F540_67682A6B_16E09F.png"),
            Academy("生命科学学院","http://smkxxy.swu.edu.cn/u/cms/smkxxy/202305/081651569ksi_517007504153cd883530859cb9409fa.jpg"),
        )

        adapter.setData(data)

    }

    inner class AcademyAdapter : RecyclerView.Adapter<AcademyViewHolder>() {

        val data = ArrayList<Academy>()

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AcademyViewHolder {
            val view =
                LayoutInflater.from(parent.context).inflate(R.layout.academy_item, parent, false)

            val holder = AcademyViewHolder(view)

            return holder;
        }

        override fun getItemCount() = data.size

        override fun onBindViewHolder(holder: AcademyViewHolder, position: Int) {
            val item = data.get(position)
            holder.render(item)
        }

        fun setData(arr: List<Academy>) {
            data.clear()
            data.addAll(arr)

            notifyItemRangeChanged(0,arr.size)
        }

    }

    inner class AcademyViewHolder(v: View) : RecyclerView.ViewHolder(v) {

        val coverView = v.findViewById<ImageView>(R.id.cover)
        val titleView = v.findViewById<TextView>(R.id.title)

        fun render(item:Academy){
            titleView.text = item.name
            // 加载网络图片
            Glide.with(activity!!).load(item.cover).into(coverView);
        }
    }


    inner class CarouseAdapter : RecyclerView.Adapter<CarouselViewHolder>() {

        val data = listOf(
            R.mipmap.s7,
            R.mipmap.s8,
            R.mipmap.s9,
            R.mipmap.s10,
            R.mipmap.s11,
            R.mipmap.s12,
        )

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CarouselViewHolder {
            val view =
                LayoutInflater.from(parent.context).inflate(R.layout.carousel_item, parent, false)

            return CarouselViewHolder(view)
        }

        override fun getItemCount() = data.size

        override fun onBindViewHolder(holder: CarouselViewHolder, position: Int) {
            val id = data.get(position)
            holder.img.setImageResource(id)
        }

    }

    inner class CarouselViewHolder(v: View) : RecyclerView.ViewHolder(v) {

        val img = v.findViewById<ImageView>(R.id.carousel_image_view)
    }

}